package io.geekidea.boot.store.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.materials.entity.Materials;
import io.geekidea.boot.materials.query.MaterialsQuery;
import io.geekidea.boot.otherstores.entity.Stores;
import io.geekidea.boot.store.dto.StoreDto;
import io.geekidea.boot.store.entity.AreaName;
import io.geekidea.boot.store.entity.OneStore;
import io.geekidea.boot.store.entity.Store;
import io.geekidea.boot.store.query.StoreQuery;
import io.geekidea.boot.store.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/store")
@Tag(name = "门店管理")
public class StoreController {

    @Resource
    private StoreService storeService;


    //逻辑删除
    @GetMapping("/del/{id}")
    @Operation(summary = "逻辑删除",description = "根据id逻辑删除对应的列")
    public ApiResult del(@PathVariable Integer id){
        boolean update = storeService.delById(id);
        if (!update) {
            return ApiResult.success("删除失败");
        }
        return ApiResult.success("删除成功");

    }


    @PostMapping("/update")
    public ApiResult updateStore(@RequestBody StoreDto storeDto){

        Store stores=new Store();
        BeanUtil.copyProperties(storeDto,stores);
        storeService.updateById(stores);
        return ApiResult.success("修改门店成功",200);
    }


    @GetMapping("/findone/{areaId}")
    @Operation(summary = "根据ID查询门店")
    public ApiResult findOne(@PathVariable Integer areaId){
        List<Store> store = storeService.getStore(areaId);

        return ApiResult.success(store);
    }

    @GetMapping("/areaname")
    @Operation(summary = "获得所有战区名")
    public ApiResult getAreaName(){
        List<AreaName> allAreaName = storeService.getAllAreaName();
        return ApiResult.success(allAreaName);
    }


    @PostMapping("/add")
    public ApiResult addStore(@RequestBody StoreDto storeDto){
        storeDto.setCreatedTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());


        int rows = storeService.addStore(storeDto);
        System.out.println(rows);
        if (rows!=0){
            return ApiResult.success("插入成功！",200);
        }else {
            return null;
        }

    }


    @PostMapping("/findAll")
    @Operation(summary = "搜索门店",description = "按照多条件组合分页查询门店列表")
    public ApiResult findAllList(@RequestBody StoreQuery storeQuery){


        Page<Store> page=new Page<>(storeQuery.getPage(),storeQuery.getSize());
        Page<Store> pageDto = storeService.headquartersPage(page, storeQuery);
        PageDto<Store> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }

    @PostMapping("/list")
    @Operation(summary = "查询门店所有数据")
    public ApiResult storeList(@RequestBody StoreQuery storeQuery){
        LambdaQueryWrapper<Store> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank("0"),Store::getStatus,"0");

        lambdaQueryWrapper.eq(StringUtils.isNotBlank(storeQuery.getId()),Store::getId,storeQuery.getId());
        List<Store> storeList = storeService.list(lambdaQueryWrapper);
       return ApiResult.success(storeList);
    }
}
